import QtQuick 2.15
import org.ukui.panel.widgets 1.0

Item {
    id: trayView
    width: childrenRect.width
    height: showView.height + 4
//    property int maxShowIconIndex: Widget.globalConfig.getValue("TrayViewShowNum") === "" ? 3 : Widget.globalConfig.getValue("TrayViewShowNum")
    property int dragStartIndex: -1
    property int maxShowIconIndex: 3

    onMaxShowIconIndexChanged: {
        showGroup.setGroupEnd(maxShowIconIndex);
        folderGroup.setGroupBegin(maxShowIconIndex + 1);
//        Widget.globalConfig.setValue("TrayViewShowNum", 3);
    }

    Row {
        spacing: 10
        Rectangle {
            width: 40
            height: 40
            color: "pink"
            anchors.right: dropArea.left
            DropArea {
                anchors.fill: parent
                onDropped: {
                    ItemModel.setOrderBetweenGroups(showGroup, showGroup.index(trayView.dragStartIndex, 0), folderGroup, folderView.folderCount)
                    maxShowIconIndex = maxShowIconIndex - 1;
                }
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    if (!folderView.visible) {
                        folderView.show();
                    } else {
                        folderView.hide();
                    }
                }
            }
        }

        ShowView {
            id: showView
            showViewEndIndex: maxShowIconIndex
            onShowDragStart: startIndex => {
                                   trayView.dragStartIndex = startIndex;
                               }
        }
    }


    FolderView {
        id: folderView
        folderBeginIndex: maxShowIconIndex + 1
        onFolderDragStart: startIndex => {
                               trayView.dragStartIndex = startIndex;
                           }

        onFolderDragFinshed: {
            if (showView.isInserting) {
                ItemModel.setOrderBetweenGroups(folderGroup, folderGroup.index(trayView.dragStartIndex, 0), showGroup, showView.insertEndIndex)
                maxShowIconIndex = maxShowIconIndex + 1;

                showView.isInserting = false;
                if (showView.indexAtNullItem() < 0) {
                    return;
                }
                showView.removeNullItem();
            }
        }

    }

}
